package com.example.planetexploder.model.folder;

import android.content.Context;
import android.util.Log;

import com.example.planetexploder.constants.LogConst;

import java.io.File;

import static com.example.planetexploder.constants.FolderConst.IMAGE_FOLDER_NAME;
import static com.example.planetexploder.constants.FolderConst.TEXT_FOLDER_NAME;

/**
 * 摘抄集文件夹操作类
 * @author 蒋子涵
 * 2019.10.15
 */
public class BookFolder {

    private File root;
    private Context context;

    public BookFolder(Context context, File root){
        this.context = context;
        this.root = root;
    }

    /**
     * 创建摘抄集文件夹
     * @param bookId  摘抄集id
     */
    public void create_BookFolder(String bookId) {
        Log.d(LogConst.MTAG, "create_BookFolder()");
        File eachBookFolder = new File(root,bookId);
        if(!eachBookFolder.exists())
            eachBookFolder.mkdir();
        File textFolder = new File(eachBookFolder,TEXT_FOLDER_NAME);
        if(!textFolder.exists())
            textFolder.mkdir();
        File photoFolder = new File(eachBookFolder,IMAGE_FOLDER_NAME);
        if(!photoFolder.exists())
            photoFolder.mkdir();
    }

    /**
     * 删除摘抄集文件夹
     * @param bookId  摘抄集id
     */
    public void delete_BookFolder(String bookId) {
        Log.d(LogConst.MTAG, "delete_BookFolder()");
        File book = new File(root,bookId);
        if(book.exists())
        {
            File[] files = book.listFiles();    //texts   photoes
            for(File each : files)
            {
                File[] items = each.listFiles();
                if(items != null)
                    for (File f : items)
                        f.delete();
                each.delete();
            }
            book.delete();
        }
    }

}
